﻿using System;
using System.Configuration;

namespace Complaint.Common
{
    /// <summary>
    /// Summary description for ConfigHelper
    /// </summary>
    public static class ConfigHelper
    {
        public static string GetConfigValue(string key)
        {
            string val = ConfigurationManager.AppSettings[key];
            if (val == null)
                throw new ConfigurationException(string.Format("AppSettings configuration entry not found for key '{0}'.", key));
            return val;
        }

        public static int GetConfigValueAsInt(string key)
        {
            return Convert.ToInt32(GetConfigValue(key));
        }

        public static bool KeyExists(string key)
        {
            return ConfigurationManager.AppSettings[key] != null;
        }

        public static bool GetConfigValueAsBool(string key)
        {
            return GetConfigValueAsBool(key, false);
        }

        public static bool GetConfigValueAsBool(string key, bool returnFalseIfMissing)
        {
            string val = ConfigurationManager.AppSettings[key];
            if (val == null)
                if (returnFalseIfMissing)
                    return false;
                else
                    throw new ConfigurationErrorsException(string.Format("AppSettings configuration entry not found for key '{0}'.", key));
            return Convert.ToBoolean(val);
        }
    }
}